<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function special_activities_list_page()
{
    @$output = drupal_render(drupal_get_form('special_activities_list_form'));
    return $output;
}
function special_activities_overview_page(){
    @$output = drupal_render(drupal_get_form('special_activities_overview_form'));
    return $output;
}

function special_activities_overview_form(){
    $form['farmerinfo']['#markup'] = special_activities_overview('farmer');
    $form['plant_info']['#markup'] = special_activities_overview('plant_info');
    $form['soil_info']['#markup'] = special_activities_overview('soil_info');
    $form['fertilizer_case']['#markup'] = special_activities_overview('fertilizer_case');
    $form['gift']['#markup'] = special_activities_overview('gift');
    $form['plant_measure']['#markup'] = special_activities_overview('plant_measure');
    $form['revisit']['#markup'] = special_activities_overview('revisit');
    $form['village_activity']['#markup'] = special_activities_overview('village_activity');
    $form['village']['#markup'] = special_activities_overview('village');
    return $form;
}

function special_activities_list_form($form, &$form_state)
{
    $form['special_activities'] = array(
        '#type' => 'textfield',
        '#title' => '专项活动',
        '#size' => 32,
        '#maxlength' => 50,
        '#id'=>'special_activities_textfield'
//        '#required' => TRUE,
    );
    $table['#markup'] =  data_list_view_page($form, $form_state);
    ajax_submit_form($form,$form_state,$table,'');
    return $form;
}

function special_activities_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
        global $user;
        $output = '';
        //表头
        $header = array(
            array('data' => '选择', 'class' => ['essential no']),
            array('data' => '序号', 'class' => ['essential no'], 'field' => 'special_activities_id'),
            array('data' => '专项活动名称', 'class' => ['essential title']),
            array('data' => '实施大区', 'class' => ['essential title']),
            array('data' => '实施地点', 'class' => ['essential title']),
            array('data' => '农民会议(场次)', 'class' => ['essential title']),
            array('data' => '经销商会议(场次)', 'class' => ['essential title']),
            array('data' => '活动特色名称', 'class' => ['essential title']),
            array('data' => '创建时间', 'class' => ['essential timestamp']),
            array('data' => '操作', 'class' => ['essential timestamp']),
        );
        $query = db_select('special_activities', 'SA')->extend('PagerDefault')->extend('TableSort');
        //专项活动
        if(!empty($form_state['values']['special_activities'])){
            $query->condition('SA.title','%'.db_like($form_state['values']['special_activities']).'%', 'LIKE');
        }

        if(!user_access('view all special_activities') && user_access('view own special_activities')){
            $query->condition('S.author_uid',$user->uid, '=');
        }
        $query->fields('SA');
        if(empty($form_state['values']) || empty($form_state['values']['order'])) {
            $query->orderBy('SA.timestamp', 'DESC');
        }
        $query->limit(variable_get("list_page_item_number", 5));

        $special_activitiesInfo = $query->orderByHeader($header)->execute();

        $rows = array();
        //表内容
        $n = 1;
        foreach ($special_activitiesInfo as $row) {
            if (nonghua_user_access('special_activities', 'delete', $row->special_activities_id, $row->author_uid, 0)) {
                $disabled = ' class="listViewTableCheck" ';
            } else{
                $disabled = ' disabled="true" ';
            }
            $rows[] = array(
                array('data' => "<input type='checkbox'".$disabled." value ='" . $row->special_activities_id . "' name='" . $row->special_activities_id . "'/>" . $n++, 'class' => 'integrant essential no'),
                array('data' => $row->special_activities_id, 'class' => ['essential no']),
                array('data' => '<a href="'.base_path().'special_activities/overview/'.$row->special_activities_id.'">'.$row->title.'</a>', 'class' => ['integrant essential title']),
                array('data' => $row->implementation_region, 'class' => ['integrant essential implementation_region']),
                array('data' => $row->implementation_place, 'class' => ['integrant essential implementation_place']),
                array('data' => $row->farmer_meeting, 'class' => ['integrant essential farmer_meeting']),
                array('data' => $row->distrbutor_meeting, 'class' => ['integrant essential distrbutor_meeting']),
                array('data' => $row->feature_name, 'class' => ['integrant essential feature_name']),
                array('data' => date("Y-m-d H:i:s",$row->timestamp), 'class' => ['integrant essential timestamp']),
                array('data' => '<a href="' . base_path() . 'special_activities/edit/' . $row->special_activities_id . '"  data-toggle="modal" target="_self">详情</a>', 'class' => ['integrant essential more']),
            );
        }


        $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
        /*	 * *按钮** */
        if(sizeof($rows) > 0 && (user_access('delete own special_activities') ||  user_access('delete all special_activities'))) {
            $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
            $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
        }
        $output .= '<a href="'.base_path().'special_activities/export?'.$str.'&experts_name='.$form_state['values']['experts_name'].'" data-toggle="modal" class="btn btn-mini">导出</a>';
        $output .= theme('pager');
        return $output;
}

function special_activities_delete()
{
    ajax_table_delete('special_activities', 'special_activities_id');
}

function special_activities_export($form,&$form_state){
    $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    $tableheader = array('专项活动名称','创建时间');
    $data = get_special_activities_data($form, $form_state);
    nonghua_php_excel($letter,$tableheader,$data,'专项活动数据');
}

function get_special_activities_data($form = '', &$form_state = ''){
    global $user;
    $user_info = _get_user_info($user->uid);
    $query = db_select('special_activities', 'SA');
    //专项活动
    if(!empty($_GET['special_activities'])){
        $query->condition('SA.title','%'.db_like($_GET['special_activities']).'%', 'LIKE');
    }

    if(!user_access('view all special_activities') && user_access('view own special_activities')){
        $query->condition('S.author_uid',$user->uid, '=');
    }
    $query->fields('SA',array('title','timestamp'));
    $expertsInfo = $query->execute()->fetchAll();
    foreach ($expertsInfo as $key => $value) {
        $expertsInfo[$key]->timestamp = date('Y-m-d H:i:s',$value->timestamp);
        // $farmerInfo[$k]->province= $v->province.$v->city.$v->county.$v->town.$v->village;
    }
    return $expertsInfo;
}

function special_activities_overview($type){
    $special_activities_id = arg(2);
    if($type == 'farmer'){
        $query = db_select('special_activities','SA');
        $query->join('farmer','F','F.special_activities_id = SA.special_activities_id');
        $query->fields('F');
        $query->fields('SA',array('title'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'plant_info'){
        $query = db_select('special_activities','SA');
        $query->join('plant_info','PI','PI.special_activities_id = SA.special_activities_id');
        $query->join('plant','P','P.plant_id = PI.plant_id');
        $query->fields('PI');
        $query->fields('SA',array('title'));
        $query->fields('P',array('plant_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'soil_info'){
        $query = db_select('special_activities','SA');
        $query->join('soil_info','SI','SI.special_activities_id = SA.special_activities_id');
        $query->join('farmer','F','F.farmer_id = SI.farmer_id');
        $query->fields('SI');
        $query->fields('SA',array('title'));
        $query->fields('F',array('farmer_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'fertilizer_case'){
        $query = db_select('special_activities','SA');
        $query->join('fertilizer_case','FC','FC.special_activities_id = SA.special_activities_id');
        $query->join('farmer','F','F.farmer_id = FC.farmer_id');
        $query->fields('FC');
        $query->fields('SA',array('title'));
        $query->fields('F',array('farmer_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'gift'){
        $query = db_select('special_activities','SA');
        $query->join('gift','G','G.special_activities_id = SA.special_activities_id');
        $query->join('farmer','F','F.farmer_id = G.farmer_id');
        $query->fields('G');
        $query->fields('SA',array('title'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->fields('F',array('farmer_name'));
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'plant_measure'){
        $query = db_select('special_activities','SA');
        $query->join('plant_measure','PM','PM.special_activities_id = SA.special_activities_id');
        $query->join('farmer','F','F.farmer_id = PM.farmer_id');
        $query->join('plant','P','P.plant_id = PM.plant_id');
        $query->fields('PM');
        $query->fields('SA',array('title'));
        $query->fields('F',array('farmer_name'));
        $query->fields('P',array('plant_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'revisit'){
        $query = db_select('special_activities','SA');
        $query->join('revisit','R','R.special_activities_id = SA.special_activities_id');
         $query->join('farmer','F','F.farmer_id = R.farmer_id');
        $query->fields('R');
        $query->fields('SA',array('title'));
        $query->fields('F',array('farmer_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'village_activity'){
        $query = db_select('special_activities','SA');
        $query->join('village_activity','VA','VA.special_activities_id = SA.special_activities_id');
        $query->join('distributor','D','D.distributor_id = VA.distributor_id');
        $query->fields('VA');
        $query->fields('SA',array('title'));
        $query->fields('D',array('distributor_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }

    if($type == 'village'){
        $query = db_select('special_activities','SA');
        $query->join('village','V','V.special_activities_id = SA.special_activities_id');
        $query->join('distributor','D','D.distributor_id = V.distributor_id');
        $query->fields('V');
        $query->fields('SA',array('title'));
        $query->fields('D',array('distributor_name'));
        $query->condition('SA.special_activities_id',$special_activities_id,'=');
        $query->range(0, 4);
        $result = $query->execute()->fetchAll();
    }
    // print_r($village_info);die;
    return $result;
}